<template>
  <div>
    <el-dialog :before-close="cancel" title="编辑会员" :visible.sync="info.isShow" center>
      <el-form ref="member" :model="memberInfo" :rules="rules">
        <el-form-item prop="phone" label="手机号" :label-width="formLabelWidth">
          <el-input v-model="memberInfo.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="nickname" label="昵称" :label-width="formLabelWidth">
          <el-input v-model="memberInfo.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码" :label-width="formLabelWidth">
          <el-input v-model="memberInfo.password" autocomplete="off"></el-input>
          <p>留空则不修改</p>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="memberInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="edit">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { memberedit, memberinfo } from "../../request/api";
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {
      memberInfo: {
        uid:"",
        phone:'',
        nickname: "",
        password: "",
        status: 1
      },
      formLabelWidth: "120px",
      rules: {
        nickname: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8个字符", trigger: "blur" }
        ]
      },
      defaultProps: {
        children: "children",
        label: "label"
      }
    };
  },
  props: ["info"],
  mounted() {
    if (this.memberlist.length == 0) {
      this.getMember();
    }
  },
  computed: {
    ...mapGetters({
      memberlist: "member/getList"
    })
  },
  methods: {
    ...mapActions({
      getMember: "member/getMemberList",
    }),
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    //清空输入的表单内容以及相关验证
    reset() {
      this.memberInfo = {
        uid:"",
        phone:'',
        nickname: "",
        password: "",
        status: 1
      };
      this.$refs.member.resetFields();
    },
    //创建一个查询一条数据事件
    lookup(uid) {
      memberinfo({ uid }).then(res => {
        if (res.code == 200) {
          this.memberInfo = res.list;
          this.memberInfo.password = ''
          this.memberInfo.id = uid;
        }
      });
    },
    //封装一个编辑事件
    edit(){
      this.$refs.member.validate(val=>{
        if(val){
          memberedit(this.memberInfo).then(res=>{
            if(res.code==200){
              this.$message.success(res.msg);
              this.cancel();
              this.getMember()
            }else{
              this.$message.error(res.msg);
            }
          })
        }
      })
    }
  }
}
</script>

<style  scoped>
</style>
